package com.yunkaweilai.android.activity.operation.consumption.guadan;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.SimpleInterface;
import com.yunkaweilai.android.model.guadan.LableModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.guadan.GuadanLableAddDialog;
import com.yunkaweilai.android.view.dialog.guadan.GuadanLableDelDialog;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 标签
 * Created by Administrator on 2017/8/9.
 */

public class GuadanLableActivity extends BaseActivity implements SimpleInterface {

    @BindView(R.id.id_listview)
    ListView idListview;
    @BindView(R.id.content_view)
    RelativeLayout contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;

    private ArrayList<LableModel.DataBean.ListBean> list = new ArrayList<>();
    private CommonAdapter<LableModel.DataBean.ListBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_company1);
        ButterKnife.bind(this);
        new TitleBuilder(this).setLeftImage(R.mipmap.ic_go_back)
                .setTitleText("挂单标签").setRightText("新增")
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                })
                .setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //新增单位
                        GuadanLableAddDialog dialog = new GuadanLableAddDialog(mAc,GuadanLableActivity.this);
                        dialog.show();
                    }
                });
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initAdapter();
        initHttp();
    }
    private void initAdapter() {
        adapter = new CommonAdapter<LableModel.DataBean.ListBean>(this,R.layout.item_list_guadan_lable,list) {
            @Override
            protected void convert(ViewHolder viewHolder, final LableModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_tv_company,item.getTitle());
                viewHolder.getView(R.id.id_img_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GuadanLableDelDialog delDialog = new GuadanLableDelDialog(mAc,GuadanLableActivity.this,item.getId());
                        delDialog.show();
                    }
                });
            }
        };
        idListview.setAdapter(adapter);
    }
    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_LABLE_LIST)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            LableModel lableModel = gson.fromJson(json, LableModel.class);
                            list.clear();
                            list.addAll(lableModel.getData().getList());
                            adapter.notifyDataSetChanged();
                            if (idMultipleStatusView != null){
                                idMultipleStatusView.showContent();
                            }
                        }else{
                            if (idMultipleStatusView != null){
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null){
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    @Override
    public void nothing() {
        idMultipleStatusView.showLoading();
        initHttp();
    }
}
